package com.suns.server;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import java.util.concurrent.TimeUnit;

/**
 * @author xiangminwen
 * @date 2024/12/5 8:38
 */
public class CaffeineTest {

    public static void main(String[] args) throws InterruptedException {
        Cache<String, Integer> caffeine = Caffeine.newBuilder()
                .initialCapacity(3)
                .maximumSize(4)
                .expireAfterWrite(60, TimeUnit.SECONDS)
//                .removalListener((key, value, cause) -> {
//                    System.out.println("key:" + key + ",valeu:" + value);
//                })
                /*          .expireAfter(new Expiry<String, Integer>() {
                              @Override
                              public long expireAfterCreate(String key, Integer value, long currentTime) {
                                  return currentTime;
                              }
                              @Override
                              public long expireAfterUpdate(String key, Integer value, long currentTime, @NonNegative long currentDuration) {
                                  return currentDuration;
                              }
                              @Override
                              public long expireAfterRead(String key, Integer value, long currentTime, @NonNegative long currentDuration) {
                                  return currentDuration;
                              }
                          })*/
                .build();

        new Thread(() -> {
            while (true) {
                for (int i = 0; i < 100000; i++) {
                    caffeine.put(String.valueOf(i), i);
                }
            }
        }).start();

        new Thread(() -> {
            while (true) {
                for (int i = 100000; i < 200000; i++) {
                    caffeine.put(String.valueOf(i), i);
                }
            }
        }).start();

        new Thread(() -> {
            while (true) {
                for (int i = 200000; i < 300000; i++) {
                    caffeine.put(String.valueOf(i), i);
                }
            }
        }).start();

        TimeUnit.MILLISECONDS.sleep(100);
        System.out.println(caffeine.estimatedSize());
        TimeUnit.MILLISECONDS.sleep(100);
        System.out.println(caffeine.estimatedSize());
        TimeUnit.MILLISECONDS.sleep(100);
        System.out.println(caffeine.estimatedSize());
        TimeUnit.MILLISECONDS.sleep(100);
        System.out.println(caffeine.estimatedSize());
        TimeUnit.MILLISECONDS.sleep(100);
        System.out.println(caffeine.estimatedSize());
//        caffeine.asMap().forEach((k, v) -> {
//            System.out.println(k + ":" + v);
//        });

//        caffeine.get("", s -> {
//
//            return 5;
//        });
        //不同的KEY设置不同的过期时间，可变的策略是否提供
//        caffeine.policy().expireVariably().ifPresent(p -> {
//            p.put("9", 9, Duration.ofSeconds(10));
//        });
//        System.out.println(caffeine.getIfPresent("9"));
        //        //绑定端口
//        ServerBuilder<?> build = ServerBuilder.forPort(9000);
//        //发布服务
//        build.addService(new HelloServiceImpl());
//        //启动服务
//        Server server = build.build();
//        server.start();
//        server.awaitTermination();
    }
}
